package com.api.customization.unionpay.market.web;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.customization.unionpay.market.service.ExternalFundingActivityService;
import com.customization.unionpay.market.service.impl.ExternalFundingActivityServiceImpl;
import com.customization.unionpay.market.util.FormDataUtils;
import com.engine.common.util.ServiceUtil;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

/**
 * @author sjm
 * @description 外部出资活动台账相关
 */
@Path("/market/external")
public class ExternalFundingActivityController {

    private static final Logger log = LoggerFactory.getLogger(ExternalFundingActivityController.class);

    private ExternalFundingActivityService getService(User user) {
        return ServiceUtil.getService(ExternalFundingActivityServiceImpl.class, user);
    }

    // 查询外部出资活动台账的可充值金额
    @POST
    @Path("/money/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String externalGetInfo(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "查询成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                String actAuthCode = parseJson.getString("ACT_AUTH_CODE");
                String source = parseJson.getString("SOURCE");

                if (StrUtil.isBlank(actAuthCode)) {
                    result.put("msg", "授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                JSONObject resultData = getService(user).externalGetInfo(actAuthCode,source);
                result.put("data", resultData);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常："+e.getMessage());
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }


    // 合同新增修改外部出资活动台账
    @POST
    @Path("/addOrUpdate/info")
    @Produces(MediaType.APPLICATION_JSON)
    public String externalUpdateApplicant(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        JSONObject result = new JSONObject();
        try {
            result.put("msg", "推送成功");
            result.put("data", null);
            result.put("code", 200);
            result.put("errorCode", 1);
            //获取当前用户
            User user = HrmUserVarify.getUser(request, response);

            String body = FormDataUtils.readBodyUTF8(request);

            log.info("请求体参数：" + body);
            if (body != null) {
                JSONObject parseJson = JSONObject.parseObject(body);

                // 公司	        gs            合同备案流程归档时写入
                String gs = parseJson.getString("gs");
                // 部门	        bm            合同备案流程归档时写入
                String bm = parseJson.getString("bm");
                // 经办人        jbr            合同备案流程归档时写入
                String jbr = parseJson.getString("jbr");
                // 申请摘要      sqzy            合同备案流程归档时写入
                String sqzy = parseJson.getString("sqzy");
                // 合同编码      htbm            合同备案流程归档时写入
                String htbm = parseJson.getString("htbm");
                // 合同名称	    htmc            合同备案流程归档时写入
                String htmc = parseJson.getString("htmc");
                // 项目编码	    PROJECT_CODE            合同备案流程归档时写入
                String projectCode = parseJson.getString("PROJECT_CODE");
                // 项目名称	    PROJECT_NAME            合同备案流程归档时写入
                String projectName = parseJson.getString("PROJECT_NAME");
                // 项目类型编码	xmlxbm            合同备案流程归档时写入
                String xmlxbm = parseJson.getString("xmlxbm");
                // 项目类型名称	xmlxmc            合同备案流程归档时写入
                String xmlxmc = parseJson.getString("xmlxmc");
                // 供应商编码	    gysbm            合同备案流程归档时写入
                String gysbm = parseJson.getString("gysbm");
                // 供应商名称	    gysmc            合同备案流程归档时写入
                String gysmc = parseJson.getString("gysmc");
                // 营销平台活动授权码    yxpthdsqm       合同备案流程归档时写入
                String yxpthdsqm = parseJson.getString("yxpthdsqm");
                // 可充值金额	         kczje        合同备案流程归档时写入合同金额
                String kczje = parseJson.getString("kczje");

                String source = parseJson.getString("SOURCE");
                String time = parseJson.getString("TIME");

                if (StrUtil.isBlank(gs)) {
                    result.put("msg", "公司为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(bm)) {
                    result.put("msg", "部门为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(jbr)) {
                    result.put("msg", "经办人为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(sqzy)) {
                    result.put("msg", "申请摘要为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(htbm)) {
                    result.put("msg", "合同编码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(htmc)) {
                    result.put("msg", "合同名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(projectCode)) {
                    result.put("msg", "项目编码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(projectName)) {
                    result.put("msg", "项目名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(xmlxbm)) {
                    result.put("msg", "项目类型编码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(xmlxmc)) {
                    result.put("msg", "项目类型名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(gysbm)) {
                    result.put("msg", "供应商编码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(gysmc)) {
                    result.put("msg", "供应商名称为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(yxpthdsqm)) {
                    result.put("msg", "营销平台活动授权码为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(kczje)) {
                    result.put("msg", "可充值金额为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(source)) {
                    result.put("msg", "来源系统为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                if (StrUtil.isBlank(time)) {
                    result.put("msg", "请求时间为空！");
                    result.put("data", null);
                    result.put("code", 500);
                    result.put("errorCode", 10003);
                    return JSONObject.toJSONString(result);
                }
                getService(user).externalUpdateInfo(gs,bm,jbr,sqzy,htbm,htmc,projectCode,projectName,xmlxbm,xmlxmc,gysbm,gysmc,yxpthdsqm,kczje);
            } else {
                result.put("msg", "参数异常");
                result.put("data", null);
                result.put("code", 500);
                result.put("errorCode", 10003);
            }
        } catch (Exception e) {
            log.info("接口异常：" + e.getMessage());
            result.put("msg", "接口异常，请联系管理员！");
            result.put("data", null);
            result.put("code", 500);
            result.put("errorCode", 5000);
        }
        return JSONObject.toJSONString(result);
    }

}
